package net.cassite.daf4j

import net.cassite.daf4j.util.{AutoTxWithReturn, AutoTx}

/**
 * rich class for query
 */
class RichQuery(private val query: Query) {
  def tx(auto: Tx => Unit): Unit = {
    query.autoTx(new AutoTx {
      override def apply(tx: Tx): Unit = auto(tx)
    })
  }

  def txRes[T](auto: Tx => T): T = {
    query.autoTx(new AutoTxWithReturn[T] {
      override def apply(tx: Tx): T = auto(tx)
    })
  }

  def txDefault[T](defaultRes: T)(auto: Tx => T): T = {
    query.autoTx(new AutoTxWithReturn[T] {
      override def apply(tx: Tx): T = auto(tx)
    }, defaultRes)
  }
}
